﻿Option Strict On
Option Explicit On

Public Enum PrivilegeFunction
  NONE = 0
  VIEW = 1
  MODIFY = 11
End Enum

Public Enum FunctionName
  ORDER_MANAGEMENT = 1
  VIEW_LOG
  ACCOUNTMANAGEMENT
  USERMANAGEMENT
  NUM_FUNCTION
End Enum

Public Enum ReportNodes
  MachineList
  ErrorList
End Enum

Public Module modGlobal
  Public MyConnectionString As String = "Server= localhost ;" & _
                                        "Database=productmanage;" & _
                                        "Uid=root;" & _
                                        "Pwd=123456;" & _
                                        "Connect Timeout=30;" & _
                                        "charset=utf8"
  Private m_AccountName As String
  Private m_PrivilegeFunction(FunctionName.NUM_FUNCTION) As PrivilegeFunction
  Public m_gReplaceTable As DataTable = CreateReplacedPartsTable(m_gReplaceTable)
  'Public m_gObjLicense As LicenseGenerator.License = Nothing

  Public Sub SetConnectString(ByVal ServerString As String)
    MyConnectionString = ServerString
  End Sub

  Public Sub CreateLicense()
    'm_gObjLicense = New LicenseGenerator.License()
  End Sub


  Private Function CreateReplacedPartsTable(ByVal dtTable As DataTable) As DataTable
    dtTable = New DataTable()
    Dim colTemporary As DataColumn = New DataColumn("Temporary")
    dtTable.Columns.Add(colTemporary)
    Dim colMachineName As DataColumn = New DataColumn("Name")
    dtTable.Columns.Add(colMachineName)
    Dim colMachineLocation As DataColumn = New DataColumn("MachineLocation")
    dtTable.Columns.Add(colMachineLocation)
    Dim colPartNumber As DataColumn = New DataColumn("PartNumber")
    dtTable.Columns.Add(colPartNumber)
    Dim colPartName As DataColumn = New DataColumn("PartName")
    dtTable.Columns.Add(colPartName)
    Dim colQuality As DataColumn = New DataColumn("Quality")
    dtTable.Columns.Add(colQuality)
    Dim colReplaceDate As DataColumn = New DataColumn("ReplaceDate")
    dtTable.Columns.Add(colReplaceDate)
    Dim colRealTimeLife As DataColumn = New DataColumn("RealTimeLife")
    dtTable.Columns.Add(colRealTimeLife)
    Dim colPartSerial As DataColumn = New DataColumn("PartSerial")
    dtTable.Columns.Add(colPartSerial)
    Dim colPartLocation As DataColumn = New DataColumn("PartLocation")
    dtTable.Columns.Add(colPartLocation)
    Dim colPartTimeLife As DataColumn = New DataColumn("PartTimeLife")
    dtTable.Columns.Add(colPartTimeLife)
    Dim colStatus As DataColumn = New DataColumn("Status")
    dtTable.Columns.Add(colStatus)
    Dim colReplacedpartID As DataColumn = New DataColumn("ID")
    dtTable.Columns.Add(colReplacedpartID)
    Return dtTable
  End Function

  Public Sub SetAccountName(ByVal Accountname As String)
    m_AccountName = Accountname
  End Sub

  Public Function GetAccountname() As String
    Return m_AccountName
  End Function

  Public Function GetAccountID() As Integer
    Dim DataBaseAccessObj As DataBaseAccess = New DataBaseAccess()
    Dim DataTable As DataTable = New DataTable()
    Dim openDB As Boolean = False
    Dim MySQLString As String = ""
    Try
      openDB = DataBaseAccessObj.OpenConnection()
      If (openDB) Then
        MySQLString = "SELECT * FROM tbl_accounts WHERE UserName ='" & modGlobal.GetAccountname() & "'"
        DataTable = New DataTable()
        DataTable = DataBaseAccessObj.GetDataTable(MySQLString)

        ' Return AccountID
        DataBaseAccessObj.CloseConnection()
        Return Convert.ToInt16(DataTable.Rows(0).Item(0).ToString())
      End If
    Catch ex As Exception
      'MessageBox.Show(My.Settings.IDS_CONNECT_DATABASE_ERROR & " " & ex.Message.ToString(), My.Settings.IDS_MACHINE_MANAGEMENT, MessageBoxButtons.OK, MessageBoxIcon.Error)

      DataBaseAccessObj.CloseConnection()
      ' Return Null
      Return -1
    End Try
  End Function

  Public Function GetAccountType() As String
    Dim DataBaseAccessObj As DataBaseAccess = New DataBaseAccess()
    Dim DataTable As DataTable = New DataTable()
    Dim openDB As Boolean = False
    Dim MySQLString As String = ""
    Try
      openDB = DataBaseAccessObj.OpenConnection()
      If (openDB) Then
        MySQLString = "SELECT * FROM tbl_accounts WHERE UserName ='" & modGlobal.GetAccountname() & "'"
        DataTable = New DataTable()
        DataTable = DataBaseAccessObj.GetDataTable(MySQLString)

        ' Return AccountID
        DataBaseAccessObj.CloseConnection()
        Return DataTable.Rows(0)("Type").ToString()
      End If
    Catch ex As Exception
      ' MessageBox.Show(My.Settings.IDS_CONNECT_DATABASE_ERROR & " " & ex.Message.ToString(), My.Settings.IDS_MACHINE_MANAGEMENT, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    DataBaseAccessObj.CloseConnection()
    ' Return Null
    Return String.Empty
  End Function

  Public Sub SetPrivilegeFunction(ByVal PriFunctionName As FunctionName, ByVal PriFunction As PrivilegeFunction)
    m_PrivilegeFunction(PriFunctionName) = PriFunction
  End Sub

  Public Function GetPrivilegeFunction(ByVal PriFunctionName As FunctionName) As PrivilegeFunction
    Return m_PrivilegeFunction(PriFunctionName)
  End Function

End Module

